
"""对发票的改造"""

from odoo import models, api, fields
from odoo.tools import float_compare
from odoo.exceptions import UserError, ValidationError



class Invoice(models.Model):
    _inherit = 'account.invoice'

    business_date = fields.Date('业务日期', default=fields.Date.context_today)
    date_invoice = fields.Date(readonly=False, states={})
    date_due = fields.Date(readonly=False, states={})

    def _nd_prepare_invoice_line_info(self, wizard_line, invoice_type, fpos, company_id):
        """继承修改，目的是让赠品的发票明细金额只能为0"""
        price_unit, account = super()._nd_prepare_invoice_line_info(wizard_line, invoice_type, fpos, company_id)
        if wizard_line.invoice_line.gift:
            price_unit = 0
        return price_unit, account

    @api.multi
    def action_invoice_open(self):
        """强制修改发票生成凭证的时间"""
        res = super().action_invoice_open()
        for invoice in self:
            invoice.move_id.write({'date': invoice.business_date})
            invoice.move_id.line_ids.write({'date_maturity': invoice.business_date})
        return res